#!/bin/sh
# PCP QA Test No. 1787
# Test pmdaopentelemetry label filtering, including optional labels
#
# Copyright (c) 2025 Red Hat.  All Rights Reserved.
#
seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.opentelemetry

_pmdaopentelemetry_check || _notrun "opentelemetry pmda and/or load generator not installed"

status=1        # failure is the default!
$sudo rm -rf $tmp $tmp.* $seq.full

_cleanup()
{
    cd $here
    $sudo rm -rf $PCP_ETC_DIR/pcp/labels/*
    _restore_config $PCP_ETC_DIR/pcp/labels
    _sighup_pmcd
    _pmdaopentelemetry_cleanup
    $sudo rm -rf $tmp $tmp.*
}

_prepare_pmda opentelemetry
trap "_cleanup; exit \$status" 0 1 2 3 15
_stop_auto_restart pmcd

_filter()
{
    # Filter a bunch of labels that we dont want for QA purposes
    sed \
    -e 's/"domainname":"[^"]*"//' \
    -e 's/"groupid":[0-9]*//' \
    -e 's/"userid":[0-9]*//' \
    -e 's/"hostname":"[^"]*"//' \
    -e 's/"machineid":"[^"]*"//' \
    -e 's/"url":"[^"]*"//' \
    -e 's/,[,]*/,/g' -e 's/,}/}/'
}

# real QA test starts here
_pmdaopentelemetry_save_config
_save_config $PCP_ETC_DIR/pcp/labels
$sudo rm -rf $PCP_ETC_DIR/pcp/labels/*

# add the URL for this test
file=$here/opentelemetry/samples/labelfiltering.txt
urlbase=`basename $file .txt | tr .- _`
echo 'file://'$file > $PCP_PMDAS_DIR/opentelemetry/config.d/$urlbase.url

# add a bunch of label filters to exercise various filtering options
cat <<EOF >>$PCP_PMDAS_DIR/opentelemetry/config.d/$urlbase.url

# Filter to remove the "uninteresting" label
FILTER: EXCLUDE LABEL random

# Filter to remove all labels in metric2, thus it becomes singular
#FILTER: EXCLUDE LABEL removeme

# Filter to exclude label "def" hence metric3 has duplicate instances
#FILTER: EXCLUDE LABEL def

#
# *** NOTE: optional labels are not implemented yet
# Filter to flag some_optional_label as optional in metric4
# (an optional label is not present in instance name, but still included as a label for the metric)
# FILTER: OPTIONAL LABEL some_optional_label

# implicit final rule includes all labels not matching anything above
# (this implicit rule is listed here just for testing purposes)
#FILTER: INCLUDE .*

EOF

_pmdaopentelemetry_install

if ! _pmdaopentelemetry_wait_for_metric opentelemetry.control.calls
then
    status=1
    exit
fi

echo; echo === URL configuration file contains
sed -e "s|$here|QA_DIR|g" < $PCP_PMDAS_DIR/opentelemetry/config.d/$urlbase.url

echo; echo === pminfo listing. Note some labels in the pminfo listing have been removed, see _filter
names=`pminfo opentelemetry | grep -F -v opentelemetry.control | LC_COLLATE=POSIX sort` 2>$tmp.err
if [ -z "$names" ]; then
    echo "FAILED - no opentelemetry metrics to show"
else
    if pminfo -fl `pminfo opentelemetry | grep -F -v opentelemetry.control | LC_COLLATE=POSIX sort` > $tmp.info 2>$tmp.err
    then
        :
    else
        echo "... failed! ... here is the Install log ..."
        cat $tmp.out
    fi
    cat $tmp.info $tmp.err | _filter
fi

echo == Note: check $seq.full for details
echo == pmdaopentelemetry LOG == >>$here/$seq.full
cat $PCP_LOG_DIR/pmcd/opentelemetry.log >>$here/$seq.full

_pmdaopentelemetry_remove

# success, all done
status=0
exit
